home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / domacnost a kancelar / autoit / autoit-v3-setup.exe / Include / Inet.au3 < prev    next >
Encoding:
Text File  |  2007-09-08  |  16.6 KB  |  422 lines

  1. #include-once
  2. ; ------------------------------------------------------------------------------
  3. ;
  4. ; AutoIt Version: 3.0
  5. ; Language:       English
  6. ; Description:    Functions that assist with Internet.
  7. ;
  8. ; ------------------------------------------------------------------------------
  9. ;===============================================================================
  10. ;
  11. ; Function Name:    _GetIP()
  12. ; Description:      Get public IP address of a network/computer.
  13. ; Parameter(s):     None
  14. ; Requirement(s):   Internet access.
  15. ; Return Value(s):  On Success - Returns the public IP Address
  16. ;                   On Failure - -1  and sets @ERROR = 1
  17. ; Author(s):        Larry/Ezzetabi & Jarvis Stubblefield
  18. ;
  19. ;===============================================================================
  20. Func _GetIP()
  21.     Local $ip, $t_ip
  22.     If InetGet("http://checkip.dyndns.org/?rnd1=" & Random(1, 65536) & "&rnd2=" & Random(1, 65536), @TempDir & "\~ip.tmp") Then
  23.         $ip = FileRead(@TempDir & "\~ip.tmp", FileGetSize(@TempDir & "\~ip.tmp"))
  24.         FileDelete(@TempDir & "\~ip.tmp")
  25.         $ip = StringTrimLeft($ip, StringInStr($ip, ":") + 1)
  26.         $ip = StringTrimRight($ip, StringLen($ip) - StringInStr($ip, "/") + 2)
  27.         $t_ip = StringSplit($ip, '.')
  28.         If $t_ip[0] = 4 And StringIsDigit($t_ip[1]) And StringIsDigit($t_ip[2]) And StringIsDigit($t_ip[3]) And StringIsDigit($t_ip[4]) Then
  29.             Return $ip
  30.         EndIf
  31.     EndIf
  32.     If InetGet("http://www.whatismyip.com/?rnd1=" & Random(1, 65536) & "&rnd2=" & Random(1, 65536), @TempDir & "\~ip.tmp") Then
  33.         $ip = FileRead(@TempDir & "\~ip.tmp", FileGetSize(@TempDir & "\~ip.tmp"))
  34.         FileDelete(@TempDir & "\~ip.tmp")
  35.         $ip = StringTrimLeft($ip, StringInStr($ip, "Your ip is") + 10)
  36.         $ip = StringLeft($ip, StringInStr($ip, " ") - 1)
  37.         $ip = StringStripWS($ip, 8)
  38.         $t_ip = StringSplit($ip, '.')
  39.         If $t_ip[0] = 4 And StringIsDigit($t_ip[1]) And StringIsDigit($t_ip[2]) And StringIsDigit($t_ip[3]) And StringIsDigit($t_ip[4]) Then
  40.             Return $ip
  41.         EndIf
  42.     EndIf
  43.     SetError(1)
  44.     Return -1
  45. EndFunc   ;==>_GetIP
  46.  
  47. ;===============================================================================
  48. ;
  49. ; Function Name:    _INetExplorerCapable()
  50. ; Description:      Convert a string to IE capable line
  51. ; Parameter(s):     $s_IEString - String to convert to a capable IExplorer line
  52. ; Requirement(s):   None
  53. ; Return Value(s):  On Success - Returns the converted string
  54. ;                   On Failure - Blank String and @error = 1
  55. ; Author(s):        Wes Wolfe-Wolvereness <Weswolf at aol dot com>
  56. ;
  57. ;===============================================================================
  58. ;
  59. Func _INetExplorerCapable($s_IEString)
  60.     If StringLen($s_IEString) <= 0 Then
  61.         Return ''
  62.         SetError(1)
  63.     Else
  64.         Local $s_IEReturn
  65.         Local $i_IECount
  66.         Local $n_IEChar
  67.         For $i_IECount = 1 To StringLen($s_IEString)
  68.             $n_IEChar = '0x' & Hex(Asc(StringMid($s_IEString, $i_IECount, 1)), 2)
  69.             If $n_IEChar < 0x21 Or $n_IEChar = 0x25 Or $n_IEChar = 0x2f Or $n_IEChar > 0x7f Then
  70.                 $s_IEReturn = $s_IEReturn & '%' & StringRight($n_IEChar, 2)
  71.             Else
  72.                 $s_IEReturn = $s_IEReturn & Chr($n_IEChar)
  73.             EndIf
  74.         Next
  75.         Return $s_IEReturn
  76.     EndIf
  77. EndFunc   ;==>_INetExplorerCapable
  78.  
  79. ;===============================================================================
  80. ;
  81. ; Function Name:    _INetGetSource()
  82. ; Description:      Gets the source from an URL without writing a temp file.
  83. ; Parameter(s):     $s_URL = The URL of the site.
  84. ; Requirement(s):   DllCall/Struct & WinInet.dll
  85. ; Return Value(s):  On Success - Returns the source code.
  86. ;                   On Failure - 0  and sets @ERROR = 1
  87. ; Author(s):        Wouter van Kesteren.
  88. ;
  89. ;===============================================================================
  90. Func _INetGetSource($s_URL, $s_Header = '')
  91.     
  92.     If StringLeft($s_URL, 7) <> 'http://' And StringLeft($s_URL, 8) <> 'https://' Then $s_URL = 'http://' & $s_URL
  93.     
  94.     Local $h_DLL = DllOpen("wininet.dll")
  95.     
  96.     Local $ai_IRF, $s_Buf = ''
  97.     
  98.     Local $ai_IO = DllCall($h_DLL, 'int', 'InternetOpen', 'str', "AutoIt v3", 'int', 0, 'int', 0, 'int', 0, 'int', 0)
  99.     If @error Or $ai_IO[0] = 0 Then
  100.         DllClose($h_DLL)
  101.         SetError(1)
  102.         Return ""
  103.     EndIf
  104.     
  105.     Local $ai_IOU = DllCall($h_DLL, 'int', 'InternetOpenUrl', 'int', $ai_IO[0], 'str', $s_URL, 'str', $s_Header, 'int', StringLen($s_Header), 'int', 0x80000000, 'int', 0)
  106.     If @error Or $ai_IOU[0] = 0 Then
  107.         DllCall($h_DLL, 'int', 'InternetCloseHandle', 'int', $ai_IO[0])
  108.         DllClose($h_DLL)
  109.         SetError(1)
  110.         Return ""
  111.     EndIf
  112.     
  113.     Local $v_Struct = DllStructCreate('udword')
  114.     DllStructSetData($v_Struct, 1, 1)
  115.     
  116.     While DllStructGetData($v_Struct, 1) <> 0
  117.         $ai_IRF = DllCall($h_DLL, 'int', 'InternetReadFile', 'int', $ai_IOU[0], 'str', '', 'int', 256, 'ptr', DllStructGetPtr($v_Struct))        
  118.         $s_Buf &= StringLeft($ai_IRF[2], DllStructGetData($v_Struct, 1))
  119.     WEnd
  120.     
  121.     DllCall($h_DLL, 'int', 'InternetCloseHandle', 'int', $ai_IOU[0])
  122.     DllCall($h_DLL, 'int', 'InternetCloseHandle', 'int', $ai_IO[0])
  123.     DllClose($h_DLL)
  124.     Return $s_Buf
  125. EndFunc   ;==>_INetGetSource
  126.  
  127. ;===============================================================================
  128. ;
  129. ; Function Name:    _INetMail()
  130. ; Description:      Open default mail client with given Address/Subject/Body
  131. ; Parameter(s):     $s_MailTo    - Address for E-Mail
  132. ;                   $s_Subject   - Subject <Weswolf at aol dot com>of E-Mail
  133. ;                   $s_MailBody  - Body of E-Mail
  134. ; Requirement(s):   _INetExplorerCapable
  135. ; Return Value(s):  On Success - Process ID of e-mail client
  136. ;                   On Failure - If Opt('RunErrorsFatal', 1)
  137. ;                                   -> Crash
  138. ;                                Else Opt('RunErrorsFatal', 0)
  139. ;                                   -> Blank String and @error = 1
  140. ; Author(s):        Wes Wolfe-Wolvereness <Weswolf at aol dot com>
  141. ;
  142. ;===============================================================================
  143. ;
  144. Func _INetMail($s_MailTo, $s_MailSubject, $s_MailBody)
  145.     Local $prev = opt("ExpandEnvStrings", 1)
  146.     Local $var = RegRead('HKCR\mailto\shell\open\command', "")
  147.     Local $ret = Run(StringReplace($var, '%1', _INetExplorerCapable('mailto:' & $s_MailTo & '?subject=' & $s_MailSubject & '&body=' & $s_MailBody)))
  148.     opt("ExpandEnvStrings", $prev)
  149.     Return $ret
  150. EndFunc   ;==>_INetMail
  151.  
  152. ;===============================================================================
  153. ;
  154. ; Function Name:    _INetSmtpMail()
  155. ; Description:      Sends an email using SMTP over TCP IP.
  156. ; Parameter(s):     $s_SmtpServer    - SMTP server to be used for sending email
  157. ;                   $s_FromName        - Name of sender
  158. ;                   $s_FromAddress    - eMail address of sender
  159. ;                   $s_ToAddress    - Address that email is to be sent to
  160. ;                   $s_Subject        - Subject of eMail
  161. ;                    $as_Body        - Single dimension array containing the body of eMail as strings
  162. ;                    $s_helo            - Helo identifier (default @COMPUTERNAME) sometime needed by smtp server
  163. ;                    $s_first        - send before Helo identifier (default @CRLF) sometime needed by smtp server
  164. ;                    $b_trace        - trace on a splash window (default 0 = no trace)
  165. ; Requirement(s):   None
  166. ; Return Value(s):  On Success - Returns 1
  167. ;                   On Failure - 0  and sets
  168. ;                                            @ERROR = 1        -    Invalid Parameters
  169. ;                                            @ERROR = 2        -    Unable to start TCP
  170. ;                                            @ERROR = 3        -    Unable to resolve IP
  171. ;                                            @ERROR = 4        -    Unable to create socket
  172. ;                                            @ERROR = 5x        -    Cannot open SMTP session
  173. ;                                            @ERROR = 50x    -    Cannot send body
  174. ;                                            @ERROR = 5000    -    Cannot close SMTP session
  175. ; Authors:        Original function to send email via TCP     - Asimzameer
  176. ;                    Conversion to UDF                        - Walkabout
  177. ;                    Correction    Helo, timeout, trace        - Jpm
  178. ;                    Correction send before Helo                - Jpm
  179. ;
  180. ;===============================================================================
  181. Func _INetSmtpMail($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_helo = "", $s_first=" ", $b_trace = 0)
  182.     
  183.     Local $v_Socket
  184.     Local $s_IPAddress
  185.     Local $i_Count
  186.     Local $s_Send[6]
  187.     Local $s_ReplyCode[6];Return code from SMTP server indicating success
  188.     
  189.     If $s_SmtpServer = "" Or $s_FromAddress = "" Or $s_ToAddress = "" Or $s_FromName = "" Or StringLen($s_FromName) > 256 Then
  190.         SetError(1)
  191.         Return 0
  192.     EndIf
  193.     If $s_helo = "" Then $s_helo = @ComputerName
  194.     If TCPStartup() = 0 Then
  195.         SetError(2)
  196.         Return 0
  197.     EndIf
  198.     StringRegExp($s_SmtpServer, "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)")
  199.     If @extended Then
  200.         $s_IPAddress = $s_SmtpServer
  201.     Else
  202.         $s_IPAddress = TCPNameToIP($s_SmtpServer)
  203.     EndIf
  204.     If $s_IPAddress = "" Then
  205.         TCPShutdown()
  206.         SetError(3)
  207.         Return 0
  208.     EndIf
  209.     $v_Socket = TCPConnect($s_IPAddress, 25)
  210.     If $v_Socket = -1 Then
  211.         TCPShutdown()
  212.         SetError(4)
  213.         Return (0)
  214.     EndIf
  215.     
  216.     $s_Send[0] = "HELO " & $s_helo & @CRLF
  217.     If StringLeft($s_helo,5) = "EHLO " Then $s_Send[0] = $s_helo & @CRLF
  218.     $s_ReplyCode[0] = "250"
  219.         
  220.     $s_Send[1] = "MAIL FROM: <" & $s_FromAddress & ">" & @CRLF
  221.     $s_ReplyCode[1] = "250"
  222.     $s_Send[2] = "RCPT TO: <" & $s_ToAddress & ">" & @CRLF
  223.     $s_ReplyCode[2] = "250"
  224.     $s_Send[3] = "DATA" & @CRLF
  225.     $s_ReplyCode[3] = "354"
  226.     
  227.     $s_Send[4] =     "From:" & $s_FromName & "<" & $s_FromAddress & ">" & @CRLF & _
  228.             "To:" & "<" & $s_ToAddress & ">" & @CRLF & _
  229.             "Subject:" & $s_Subject & @CRLF & _
  230.             "Mime-Version: 1.0" & @CRLF & _
  231.             "Content-Type: text/plain; charset=US-ASCII" & @CRLF & _
  232.             @CRLF
  233.     $s_ReplyCode[4] = ""
  234.     
  235.     $s_Send[5] = @CRLF & "." & @CRLF
  236.     $s_ReplyCode[5] = "250"
  237.     
  238.     ; open stmp session
  239.     If _SmtpSend($v_Socket, $s_Send[0], $s_ReplyCode[0], $b_trace, "220", $s_first) Then
  240.         SetError(50)
  241.         Return 0
  242.     EndIf
  243.     ; send header
  244.     For $i_Count = 1 To UBound($s_Send) - 2
  245.         If _SmtpSend($v_Socket, $s_Send[$i_Count], $s_ReplyCode[$i_Count], $b_trace) Then
  246.             SetError(50 + $i_Count)
  247.             Return 0
  248.         EndIf
  249.     Next
  250.     
  251.     ; send body records (a record can be multiline : take care of a subline beginning with a dot should be ..)
  252.     For $i_Count = 0 To UBound($as_Body) - 1
  253.         ; correct line beginning with a dot
  254.         If StringLeft($as_Body[$i_Count], 1) = "." Then $as_Body[$i_Count] = "." & $as_Body[$i_Count]
  255.         
  256.         If _SmtpSend($v_Socket, $as_Body[$i_Count] & @CRLF, "", $b_trace) Then
  257.             SetError(500 + $i_Count)
  258.             Return 0
  259.         EndIf
  260.     Next
  261.     
  262.     ; close the smtp session
  263.     $i_Count = UBound($s_Send) - 1
  264.     If _SmtpSend($v_Socket, $s_Send[$i_Count], $s_ReplyCode[$i_Count], $b_trace) Then
  265.         SetError(5000)
  266.         Return 0
  267.     EndIf
  268.     
  269.     TCPCloseSocket($v_Socket)
  270.     TCPShutdown()
  271.     Return 1
  272. EndFunc   ;==>_INetSmtpMail
  273.  
  274. ; internals routines----------------------------------
  275. Func _SmtpTrace($str, $timeout = 0)
  276.     Local $W_TITLE = "SMTP trace"
  277.     Local $g_smtptrace = ControlGetText($W_TITLE, "", "Static1")
  278.     $str = StringLeft(StringReplace($str, @CRLF, ""), 70)
  279.     $g_smtptrace &= @HOUR & ":" & @MIN & ":" & @SEC & " " & $str & @LF
  280.     If WinExists($W_TITLE) Then
  281.         ControlSetText($W_TITLE, "", "Static1", $g_smtptrace)
  282.     Else
  283.         SplashTextOn($W_TITLE, $g_smtptrace, 400, 500, 500, 100, 4 + 16, "", 8)
  284.     EndIf
  285.     If $timeout Then Sleep($timeout * 1000)
  286. EndFunc   ;==>_SmtpTrace
  287.  
  288. Func _SmtpSend($v_Socket, $s_Send, $s_ReplyCode, $b_trace, $s_IntReply="", $s_first="")
  289.     Local $s_Receive, $i, $timer
  290.     If $b_trace Then _SmtpTrace($s_Send)
  291.         
  292.     If $s_IntReply <> ""  Then
  293.  
  294.         ; Send special first char to awake smtp server
  295.         If $s_first <> -1 Then
  296.             If TCPSend($v_Socket, $s_first) = 0 Then
  297.                 TCPCloseSocket($v_Socket)
  298.                 TCPShutdown()
  299.                 Return 1; cannot send
  300.             EndIf
  301.         EndIf
  302.         
  303.         ; Check intermediate reply before HELO acceptation
  304.         $s_Receive = ""
  305.         $timer = TimerInit()
  306.         While StringLeft($s_Receive,StringLen($s_IntReply)) <> $s_IntReply And TimerDiff($timer) < 45000
  307.             $s_Receive = TCPRecv($v_Socket, 1000)
  308.             If $b_trace And $s_Receive <> "" Then _SmtpTrace("intermediate->" & $s_Receive)
  309.         WEnd
  310.     EndIf
  311.     
  312.     ; Send string.
  313.     If TCPSend($v_Socket, $s_Send) = 0 Then
  314.         TCPCloseSocket($v_Socket)
  315.         TCPShutdown()
  316.         Return 1; cannot send
  317.     EndIf
  318.  
  319.     $timer = TimerInit()
  320.     
  321.     $s_Receive = ""
  322.     While $s_Receive = "" And TimerDiff($timer) < 45000
  323.         $i += 1
  324.         $s_Receive = TCPRecv($v_Socket, 1000)
  325.         If $s_ReplyCode = "" Then ExitLoop
  326.     WEnd
  327.     
  328.     If $s_ReplyCode <> "" Then
  329.         ; Check replycode
  330.         If $b_trace Then _SmtpTrace($i & " <- " & $s_Receive)
  331.         
  332.         If StringLeft($s_Receive, StringLen($s_ReplyCode)) <> $s_ReplyCode Then
  333.             TCPCloseSocket($v_Socket)
  334.             TCPShutdown()
  335.             If $b_trace Then _SmtpTrace("<-> " & $s_ReplyCode, 5)
  336.             Return 2; bad receive code
  337.         EndIf
  338.     EndIf
  339.     
  340.     Return 0
  341. EndFunc   ;==>_SmtpSend
  342.  
  343. ;===============================================================================
  344. ;
  345. ; Description:      Resolves IP adress to Hostname
  346. ; CallTip:            _TCPIpToName($sIp, [$iOption = 0], [$hDll_Ws2_32 = "Ws2_32.dll"])
  347. ; Parameter(s):     $sIp - Ip Adress in dotted (v4) Format
  348. ;                    $iOption - Optional, Default = 0
  349. ;                        0 = Return String Hostname
  350. ;                        1 = Return Array (see Notes)
  351. ;                   $hDll_Ws2_32 - Optional, Handle to Ws2_32.dll
  352. ; Requirement(s):   AutoIt 3.2.1.12+, Successfull TCPStartup
  353. ; Return Value(s):  On Success - Hostname or Array (see Notes)
  354. ;                   On Failure - ""  and Set
  355. ;                                   @ERROR to:  1 - inet_addr DllCall Failed
  356. ;                                               2 - inet_addr Failed
  357. ;                                               3 - gethostbyaddr DllCall Failed
  358. ;                                                4 - gethostbyaddr Failed, WSAGetLastError = @Extended
  359. ;                                                5 - gethostbyaddr Failed, WSAGetLastError Failed
  360. ;                                                6 - strlen/sZStringRead Failed
  361. ;                                                7 - Error reading Aliases Array
  362. ; Author(s):        Florian Fida
  363. ; Note(s):            A successfull WSAStartup (Done by TCPStartup) is required.
  364. ;                    if $iOption = 1 then the returned Array looks Like this:
  365. ;                        $aResult[0] = Number of elemets
  366. ;                        $aResult[1] = "Hostname"
  367. ;                        $aResult[2] = "Alias 1"
  368. ;                        $aResult[3] = "Alias 2"
  369. ;                        ...
  370. ;
  371. ;===============================================================================
  372.  
  373. Func _TCPIpToName($sIp, $iOption = Default, $hDll_Ws2_32 = Default)
  374.     Local $vbinIP, $vaDllCall, $vptrHostent, $vHostent, $sHostnames, $vh_aliases, $i
  375.     Local $INADDR_NONE = 0xffffffff, $AF_INET = 2, $sSeperator = @CR
  376.     If $iOption = Default Then $iOption = 0
  377.     If $hDll_Ws2_32 = Default Then $hDll_Ws2_32 = "Ws2_32.dll"
  378.     $vaDllCall = DllCall($hDll_Ws2_32, "long", "inet_addr", "str", $sIp)
  379.     If @error Then Return SetError(1, 0, "") ; inet_addr DllCall Failed
  380.     $vbinIP = $vaDllCall[0]
  381.     If $vbinIP = $INADDR_NONE Then Return SetError(2, 0, "") ; inet_addr Failed
  382.     $vaDllCall = DllCall($hDll_Ws2_32, "ptr", "gethostbyaddr", "long_ptr", $vbinIP, "int", 4, "int", $AF_INET)
  383.     If @error Then Return SetError(3, 0, "") ; gethostbyaddr DllCall Failed
  384.     $vptrHostent = $vaDllCall[0]
  385.     If $vptrHostent = 0 Then
  386.         $vaDllCall = DllCall($hDll_Ws2_32, "int", "WSAGetLastError")
  387.         If @error Then Return SetError(5, 0, "") ; gethostbyaddr Failed, WSAGetLastError Failed
  388.         Return SetError(4, $vaDllCall[0], "") ; gethostbyaddr Failed, WSAGetLastError = @Extended
  389.     EndIf
  390.     $vHostent = DllStructCreate("ptr;ptr;short;short;ptr", $vptrHostent)
  391.     $sHostnames = __TCPIpToName_szStringRead(DllStructGetData($vHostent, 1))
  392.     If @error Then Return SetError(6, 0, $sHostnames) ; strlen/sZStringRead Failed
  393.     If $iOption = 1 Then
  394.         $sHostnames &= $sSeperator
  395.         For $i = 0 To 63 ; up to 64 Aliases
  396.             $vh_aliases = DllStructCreate("ptr", DllStructGetData($vHostent, 2) + ($i * 4))
  397.             If DllStructGetData($vh_aliases, 1) = 0 Then ExitLoop ; Null Pointer
  398.             $sHostnames &= __TCPIpToName_szStringRead(DllStructGetData($vh_aliases, 1))
  399.             If @error Then
  400.                 SetError(7) ; Error reading array
  401.                 ExitLoop
  402.             EndIf
  403.         Next
  404.         Return StringSplit(StringStripWS($sHostnames, 2), @CR)
  405.     Else
  406.         Return $sHostnames
  407.     EndIf
  408. EndFunc   ;==>_TCPIpToName
  409.  
  410. ; Internal
  411. Func __TCPIpToName_szStringRead($iszPtr, $iLen = -1, $hDll_msvcrt = "msvcrt.dll")
  412.     Local $aStrLen, $vszString
  413.     If $iszPtr < 1 Then Return "" ; Null Pointer
  414.     If $iLen < 0 Then
  415.         $aStrLen = DllCall($hDll_msvcrt, "int:cdecl", "strlen", "ptr", $iszPtr)
  416.         If @error Then Return SetError(1, 0, "") ; strlen Failed
  417.         $iLen = $aStrLen[0] + 1
  418.     EndIf
  419.     $vszString = DllStructCreate("char[" & $iLen & "]", $iszPtr)
  420.     If @error Then Return SetError(2, 0, "")
  421.     Return SetError(0, $iLen, DllStructGetData($vszString, 1))
  422. EndFunc   ;==>__TCPIpToName_szStringRead